package android_serialport_api;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)



import android.app.Activity;
import android.util.Log;

import com.lgd.warehousemanagement.Utils.DataUtils;



import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.PriorityQueue;
import org.greenrobot.eventbus.EventBus;

public class SerialPortUtils {
    private static final String TAG = "SerialPortUtils";
    public PriorityQueue<String> dataqueue = new PriorityQueue();
    public SerialPortUtils instance;
    private SerialPort serialPort = null;
    private InputStream inputStream = null;
    private OutputStream outputStream = null;
    private ReceiveThread mReceiveThread = null;
    private boolean isStart = false;
    private Activity mAtivity;

    public SerialPortUtils getInstance() {
        this.instance = new SerialPortUtils();
        return this.instance;
    }

    public SerialPortUtils() {
    }
    //  打开 串口
    public void openSerialPort(int index) {
        String[] ports = new String[]{"/dev/ttyGS3", "/dev/ttyGS2", "/dev/ttyGS1", "/dev/ttyGS0", "/dev/ttyS0", "/dev/ttyS1", "/dev/ttyS2", "/dev/ttyS3", "/dev/ttyS4"};

        try {
            this.serialPort = new SerialPort(new File(ports[index]), 115200, 0);
        } catch (Exception var4) {
            var4.printStackTrace();
            Log.d(TAG, "串口打开失败");
        }
        Log.d(TAG, "--串口打开 成功--");
        EventBus.getDefault().post("串口打开成功");
        this.inputStream = this.serialPort.getInputStream();
        this.outputStream = this.serialPort.getOutputStream();
        this.isStart = true;
        this.getSerialPort();
    }
    //  关闭 串口
    public void closeSerialPort() {
        Log.i("test", "关闭串口");
        try {
            if (this.inputStream != null) {
                this.inputStream.close();
            }
            if (this.outputStream != null) {
                this.outputStream.close();
            }
            this.isStart = false;
        } catch (IOException var2) {
            var2.printStackTrace();
        }

    }
    //  发送 串口数据
    public void sendSerialPort(String data) {
        try {
            byte[] sendData = DataUtils.HexToByteArr(data);
            this.outputStream.write(sendData);
            this.outputStream.flush();
        } catch (IOException var3) {
            var3.printStackTrace();
        }
    }

    private void getSerialPort() {
        if (this.mReceiveThread == null) {
            this.mReceiveThread = new ReceiveThread();
        }
        this.mReceiveThread.start();
    }

    private class ReceiveThread extends Thread {
        private ReceiveThread() {
        }
        public void run() {
            super.run();

            while(SerialPortUtils.this.isStart) {
                if (SerialPortUtils.this.inputStream == null) {
                    return;
                }
                byte[] readData = new byte[1024];
                try {
                    int size = SerialPortUtils.this.inputStream.read(readData);
                    String readString = DataUtils.ByteArrToHex(readData, 0, size);
//                    SerialPortUtils.this.dataqueue.add(readString);
                    EventBus.getDefault().post(readString);
                } catch (IOException var4) {
                    var4.printStackTrace();
                }
            }

        }
    }
}

